﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Easy_101_IsSymmetric
{
    public class TreeNode
    {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
        {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    class MainProject
    {
        static void Main(string[] args)
        {
            //[1,2,2,null,3,null,3]
            //TreeNode treeNode = new TreeNode(3, new TreeNode(1, null, null), new TreeNode(2, null, null));
            TreeNode treeNode = new TreeNode(1,new TreeNode(2,null,new TreeNode(3)),new TreeNode(2,null,new TreeNode(3)));
            Console.WriteLine(IsSymmetric(treeNode));
            Console.ReadKey();
        }

        public static bool IsSymmetric(TreeNode root)
        {
            bool isOver = true;
            GetTreeNode(root.left, root.right, ref isOver);
            return isOver;
        }

        public static void GetTreeNode(TreeNode leftNode,TreeNode rightNode, ref bool isOver)
        {
            if (leftNode != null && rightNode != null)
            {
                if (leftNode.val != rightNode.val)
                {
                    isOver = false;
                    return;
                }
                else
                {
                    GetTreeNode(leftNode.left, rightNode.right, ref isOver);
                    GetTreeNode(leftNode.right, rightNode.left, ref isOver);
                    isOver = isOver == false?false:true;
                }
            }
            else if (leftNode == null && rightNode == null)
            {
                isOver = isOver == false ? false : true;
            }
            else
            {
                isOver = false;
                return;
            }
        }
    }
}
